package com.wzy.vx.service.impl;

import com.wzy.vx.DTO.CateDTO;
import com.wzy.vx.dataobject.ProductInfo;
import com.wzy.vx.enums.ProductEnum;
import com.wzy.vx.enums.ProductStatusEnum;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.repository.ProductInfoRepository;
import com.wzy.vx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.service.impl
 * @ClassName: ProductServiceImpl
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/12 13:54
 * @Version: 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductInfoRepository repository;

//    查询一个
    @Override
    public ProductInfo findOne(String productId) {
        return repository.findById(productId).orElse(null);
    }
//所有状态码
    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductEnum.UP.getCode());
    }
//发现所有 带分页
    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }
//保存data
    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }
//增加库存
    @Override
    public void increaseStock(List<CateDTO> cateDTOList) {
        for (CateDTO cate: cateDTOList) {
            ProductInfo productInfo = repository.findById(cate.getProductId()).orElse(null);
            if (productInfo == null){
                //w物品不存在
                throw  new SellException(ResuletEnum.PRODUCT_NOT_EXIST);
            }
            //增加库存
            Integer result = productInfo.getProductStock()+cate.getProductQ();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }

    }
//减少库存
    @Override
    @Transactional
    public void descStock(List<CateDTO> cateDTOList) {
    for (CateDTO cateDTO:cateDTOList){
        ProductInfo productInfo = repository.findById(cateDTO.getProductId()).orElse(null);
        if (productInfo == null){
        throw new SellException(ResuletEnum.PRODUCT_NOT_EXIST);
        }
        Integer result = productInfo.getProductStock() - cateDTO.getProductQ();
        if (result < 0 ){
            throw  new SellException(ResuletEnum.PRODUCT_STOCK_ERROR);
        }
        productInfo.setProductStock(result);
        repository.save(productInfo);
    }
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).orElse(null);
        if (productInfo == null){
            throw  new SellException(ResuletEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN){
            throw new SellException(ResuletEnum.PRODUCT_STATUS_ERROR);
        }
        //如果都正确 更新
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return  repository.save(productInfo);
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).orElse(null);
        if (productInfo == null){
            throw  new SellException(ResuletEnum.PRODUCT_NOT_EXIST);
        }
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP){
            throw new SellException(ResuletEnum.PRODUCT_STATUS_ERROR);
        }
        //如果都正确 更新
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return  repository.save(productInfo);

    }
}
